package com.example.itshome.network;

import android.app.Activity;
import android.content.Intent;
import com.example.itshome.adapter.JoinedOrganizationRecycleViewAdapter;
import com.example.itshome.entity.CircleEntity;
import com.example.itshome.entity.OrganizationEntity;
import com.example.itshome.manager.UserInfoManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GetJoinedOrganizationRequest extends AbstractRecycleVIewGetRequest<OrganizationEntity>{

    public GetJoinedOrganizationRequest(Activity activity) {
        super(activity);
    }

    @Override
    protected String getPathAndParams() {
        return "organization/joined?username="+ UserInfoManager.getInstance().getUser().getUsername();
    }

    @Override
    protected void handleResult(String result) {
        try {
            List<OrganizationEntity> organizationEntities=new ArrayList<>();
            JSONObject jsonObject=new JSONObject(result);
            int code=jsonObject.getInt("code");
            if(code!=200){
                return;
            }
            JSONArray data=jsonObject.getJSONArray("data");
            int len=data.length();
            if(len==0){
                return;
            }
            for (int i=0;i<len;i++){
                JSONObject item=data.getJSONObject(i);
                OrganizationEntity entity=new OrganizationEntity()
                                        .setName(item.getString("oname"))
                                        .setJoinDate(item.getString("partInTime"))
                                        .setPictureSrc(DOMAIN_HOST+ IMAGES+item.getString("cover"));
                organizationEntities.add(entity);
            }
            setHandleResultRunnable(new Runnable() {
                @Override
                public void run() {
                    for(OrganizationEntity entity:organizationEntities){
                        addItem(entity);
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
